package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *服务
 * </p>
 *
 * @author 耿志超
 * @since 2024/12/24
 */
@Service
@Transactional
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve>implements IServeService {
	
	@Resource
	private ServeItemMapper serveItemMapper;
	@Resource
	private RegionMapper regionMapper;
	
	
	
	
	@Override
	
	public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
		//调用mapper查询数据，这里由于继承了ServiceImpl<ServeMapper, Serve>，使用baseMapper相当于使用ServeMapper
		PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO,
				() -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
		return serveResDTOPageResult;
	}
	
	@Override
	@Transactional
	public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
		for(ServeUpsertReqDTO serveUpsertReqDTO:serveUpsertReqDTOList){
			//1.校验服务是否是启用状态，若不是启用，则无法新增
			ServeItem serveItem=serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
			//如果服务未启用或者不存在
			if(ObjectUtil.isNull(serveItem)||serveItem.getActiveStatus()!=
					            FoundationStatusEnum.ENABLE.getStatus()){
				throw new ForbiddenOperationException("该服务未启用不能添加到区域下使用");
				
			
			}
			
			//2.校验是否重读新增  在数据库表中是否存在相同的   区域id和服务id两个都一样
			Integer count=lambdaQuery()
					.eq(Serve::getRegionId,serveUpsertReqDTO.getRegionId())
					.eq(Serve::getServeItemId,serveUpsertReqDTO.getServeItemId())
					.count();
			if(count>0){
				throw new ForbiddenOperationException(serveItem.getName()+"该项目已经存在");
			}
			
			//3.新增服务
			Serve serve= BeanUtil.toBean(serveUpsertReqDTO,Serve.class);
			Region region=regionMapper.selectById(serveUpsertReqDTO.getRegionId());
			serve.setCityCode(region.getCityCode());
			baseMapper.insert(serve);
			
		}
		
		
		
		
		
	}
	
	@Override
	@Transactional
	public Serve update(long id, BigDecimal price) {
		boolean update=lambdaUpdate()
				.eq(Serve::getId,id)
				.set(Serve::getPrice,price)
				.update();
		
		if(!update){
			throw new CommonException("修改价格失败");
		}
		
		
		return baseMapper.selectById(id);
	}
	
	@Override
	@Transactional
	public Serve onSale(Long id) {
		
		//查询是否存在
		Serve serve=baseMapper.selectById(id);
		if(ObjectUtil.isNull(serve)){
			throw new ForbiddenOperationException("该区域服务不存在");
		}
		//上架状态
		Integer saleStatus=serve.getSaleStatus();
		//草稿或下架状态方可上架
		if(!(saleStatus==FoundationStatusEnum.INIT.getStatus()||saleStatus==FoundationStatusEnum.DISABLE.getStatus())){
		throw new ForbiddenOperationException("该服务处于草稿或者下架状态方可下架");
		
		}
		
		
	
		
		//服务项id
		Long serveItemId= serve.getServeItemId();
		ServeItem serveItem=serveItemMapper.selectById(serveItemId);
		if(ObjectUtil.isNull(serveItem)){
			throw new ForbiddenOperationException("该服务项不存在");
		}
		
		//服务项的启用状态
		Integer activeStatus =serveItem.getActiveStatus();
		//服务项为启用状态方可上架
		if(!(FoundationStatusEnum.ENABLE.getStatus()==activeStatus)){
			throw new ForbiddenOperationException("服务项为启用状态方可上架服务");
		}
		
		//更新上架状态
		boolean update=lambdaUpdate()
				.eq(Serve::getId,id)
				.set(Serve::getSaleStatus,FoundationStatusEnum.ENABLE.getStatus())
				.update();
		if(!update){
			throw new CommonException("启动服务失败");
		}
		
		
		return baseMapper.selectById(id);
	}
	
	@Override
	public void deleteRegionServe(Long id) {
		Serve serve=baseMapper.selectById(id);
		//区域服务不存在情况
		if (ObjectUtil.isNull(serve)){
			throw new ForbiddenOperationException("区域服务不能为空");
		}
		//区域服务的状态为草稿则可以删除
		if(!(FoundationStatusEnum.INIT.getStatus()==serve.getSaleStatus())){
			throw new ForbiddenOperationException("区域服务为草稿才可以删除");
			
		}
		
		baseMapper.deleteById(id);
		
	}
	
	
	@Override
	@Transactional
	public Serve offSale(Long id){
		//1.服务不存在的情况
		Serve serve=baseMapper.selectById(id);
		if(ObjectUtil.isNull(serve)){
			throw  new ForbiddenOperationException("该服务不存在");
		}
		//2.区域服务状态不是上架状态
		if(!(serve.getSaleStatus()==FoundationStatusEnum.ENABLE.getStatus())){
			throw  new ForbiddenOperationException("该服务不是上架状态，无法下架");
			
		}
		
		//3.更新下架状态
		boolean update=lambdaUpdate()
				.eq(Serve::getId,id)
				.set(Serve::getSaleStatus,FoundationStatusEnum.DISABLE.getStatus())
				.update();
		
		if(!update){
			throw new CommonException("服务启动失败");
		}
		
		
		return baseMapper.selectById(id);
	}
	
	@Override
	@Transactional
	public void changeHot(Long id,Integer flag) {
		//1.判断是否存在该区域
		Serve serve=baseMapper.selectById(id);
		if(ObjectUtil.isNull(serve)){
			throw new ForbiddenOperationException("该服务不存在");
		}
		
		//2.设置热门
		LambdaUpdateWrapper<Serve>updateWrapper= Wrappers.<Serve>lambdaUpdate()
				.eq(Serve::getId,id)
				.set(Serve::getIsHot,flag)
				.set(Serve::getHotTimeStamp,System.currentTimeMillis());
		
		super.update(updateWrapper);
		
	}
	
	@Override
	public int querycountByRegionIdAndsaleStatus(Long id, Integer saleStatus) {
		LambdaQueryWrapper<Serve>queryWrapper= Wrappers.<Serve>lambdaQuery()
				.eq(Serve::getRegionId,id)
				.eq(ObjectUtil.isNotEmpty(saleStatus),Serve::getSaleStatus,saleStatus);
		return baseMapper.selectCount(queryWrapper);
				
		
		
	}
	
	
	@Override
	@Transactional
	public int queryServeCountByRegionIdAndSaleStatus(Long id, Integer saleStatus){
		LambdaQueryWrapper<Serve>queryWrapper=Wrappers.<Serve>lambdaQuery()
				.eq(Serve::getRegionId,id)
				.eq(ObjectUtil.isNotEmpty(saleStatus),Serve::getSaleStatus,saleStatus);
		
		
		
		
		return baseMapper.selectCount(queryWrapper);
	}
	
}
